import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
export 'dart:io';

class ImagePickerUtil {
  BuildContext _context;
  Function(File) _complete;

  ImagePickerUtil(BuildContext context,Function(File) complete){
    _context = context;
    _complete = complete;
  }

  ///底部弹框
  pickImage() async {
    showModalBottomSheet(context: _context, builder: (context) => Container(
      height: 120,
      child: Column(
        children: <Widget>[
          _takePhotoItem('拍照',true),
          _takePhotoItem('从相册选择',false),
        ],
      ),
    ));
  }

  _takePhotoItem(String title, bool isTakePhoto) {
    return GestureDetector(
      child: ListTile(
        leading: Icon(
          isTakePhoto ? Icons.camera_alt : Icons.photo_library,
        ),
        title: Text(title),
        onTap: () => _getImage(isTakePhoto),
      ),
    );
  }

  Future _getImage(bool isTakePhoto) async {
    Navigator.of(_context).pop(false);
    File image = await ImagePicker.pickImage(
        source: isTakePhoto ? ImageSource.camera :ImageSource.gallery
    );
    if(_complete != null){
      _complete(image);
    }
  }
}